﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net.Mail;

/// <summary>
/// Summary description for Helper
/// </summary>
public class Helper
{
    //Image name template
    public static string GenerateUniqueFileName()
    {
        return String.Format("{0}{1}{2}{3}{4}{5}",
                              DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day,
                              DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Millisecond);
    }//GenerateUniqueFileName()

    public static String ConvertToSqlDateTime(String sDate)
    {
        DateTime tmpDate = new DateTime();
        
        try
        {
            tmpDate = DateTime.ParseExact(sDate, "dd.MM.yyyy HH:mm",System.Globalization.CultureInfo.InvariantCulture);
        }
        catch(Exception ex)
        {
            //Dal.arParams.Clear();
            //Dal.arParams.Add(sDate+"    "+ex.Message);
            //Dal.ExecuteDBsp("Admin_AddError", Dal.arParams);

            try
            {
                tmpDate = DateTime.ParseExact(sDate, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
            }
            catch { tmpDate = DateTime.Parse(sDate); }
            
        }
        return string.Format("{0}-{1}-{2} {3}", tmpDate.Year.ToString(), tmpDate.Month.ToString().PadLeft(2, '0'), tmpDate.Day.ToString().PadLeft(2, '0'), tmpDate.ToShortTimeString());
    }

    public static string ConvertStringFromSql(string sInput)
    {
        return sInput.Replace("<br />", "\r\n");
    }

    public static string ConvertStringToSql(string sInput)
    {
        return sInput.Replace("\r\n", "<br />");
    }

    /// <summary>
    /// Sends the email.
    /// </summary>
    /// <param name="FROM">The FROM.</param>
    /// <param name="FROM">The FROM Name.</param>
    /// <param name="TO">The TO.</param>
    /// <param name="SUBJECT">The SUBJECT.</param>
    /// <param name="BODY">The BODY.</param>
    public static void SendEmail(String sFrom, String sFromName, String sTo, String sSubject,int QuestionID, StringBuilder sBody)
    {
        //try
        //{
        //    sBody.Replace("'", "\"");
        //    Dal.arParams.Clear();
        //    Dal.arParams.Add(sFrom);
        //    Dal.arParams.Add(sFromName);
        //    Dal.arParams.Add(sSubject);
        //    Dal.arParams.Add(QuestionID);
        //    Dal.arParams.Add(sBody.ToString());
        //    Dal.ExecuteDBsp("Admin_SetNewMail", Dal.arParams,true);
        //}
        //finally
        //{
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("info@rondodance.com");
                mail.To.Add(sTo);
                mail.Subject = sSubject;
                mail.Body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"><title>Message</title></head><body dir=\"ltr\">" + sBody.ToString() + "</body></html>";

                mail.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                mail.IsBodyHtml = true;
                SmtpClient SMTP = new SmtpClient("localhost");

                //System.Net.NetworkCredential SMTPUserInfo =
                //    new System.Net.NetworkCredential("admin@medserver.co.il", "qixuvgaydftcs");
                //SMTP.UseDefaultCredentials = false;
                //SMTP.Credentials = SMTPUserInfo;

                SMTP.Send(mail);
            }
            catch(Exception ex)
            {
                Dal.arParams.Clear();
                Dal.arParams.Add(ex.Message);
                Dal.ExecuteDBsp("Admin_AddError", Dal.arParams, true);
            }
        //}
    }

    /// <summary>
    /// Sends the email.
    /// </summary>
    /// <param name="FROM">The FROM.</param>
    /// <param name="FROM">The FROM Name.</param>
    /// <param name="TO">The TO.</param>
    /// <param name="SUBJECT">The SUBJECT.</param>
    /// <param name="BODY">The BODY.</param>
    public static void SendEmail(String sFrom, String sFromName, String sTo, String sSubject, StringBuilder sBody)
    {
        try
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("info@rondodance.com");
            mail.To.Add(sTo);
            mail.Subject = sSubject;
            mail.Body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"><title>Message</title></head><body dir=\"ltr\">" + sBody.ToString() + "</body></html>";

            mail.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.Normal;
            SmtpClient SMTP = new SmtpClient("localhost", 25);

            System.Net.NetworkCredential SMTPUserInfo =
                new System.Net.NetworkCredential("info@rondodance.com", "gwvfqdoeyrjl2hmtksxa");
            SMTP.UseDefaultCredentials = false;
            SMTP.Credentials = SMTPUserInfo;

            SMTP.Send(mail);
            //SmtpMail.SmtpServer.Insert(0, "smtpout.secureserver.net");
            //SmtpMail.SmtpServer = "mail.artcube.co.il.e-publish.co.il";
        }
        catch (Exception ex)
        {
            Dal.arParams.Clear();
            Dal.arParams.Add(ex.Message);
            Dal.ExecuteDBsp("Admin_AddError", Dal.arParams, true);
        }
    }

    /// <summary>
    /// Handles Error.
    /// </summary>
    /// <param name="ErrorData">The error data.</param>
    /// <param name="Key">The key.</param>
    public void SendEmail_Instant(String sFrom, String sFromName, String sTo, String sSubject, StringBuilder sBody, String sDirection)
    {
        //Regex rexEmail = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

        //String sDir = "rtl";
        //if (sDirection == "ltr")
        //    sDir = "ltr";

        //MailMessage NewMessage = new MailMessage();

        //NewMessage.From = new MailAddress(sFrom);

        ////set TO email addresses-----------------------------------
        //String[] sToMails = sTo.Split(';');
        //for (Int32 i = 0; i < sToMails.Length; i++)
        //{
        //    if (rexEmail.IsMatch(sToMails[i]))
        //        NewMessage.To.Add(sToMails[i]);
        //}
        ////---------------------------------------------------------

        //NewMessage.Subject = sSubject;
        //NewMessage.Body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"><title>Message</title></head><body dir=\"" + sDir + "\">" + sBody.ToString() + "</body></html>";

        //NewMessage.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        //NewMessage.IsBodyHtml = true;
        //NewMessage.Priority = MailPriority.Normal;
        //SmtpClient SMTP = new SmtpClient();
        //if (System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString() == "127.0.0.1")
        //    SMTP.Host = "127.0.0.1"; //ip or address
        //else
        //    SMTP.Host = "10.10.1.69"; //ip or address
        //SMTP.Port = 25;
        //if (NewMessage.To.Count > 0)
        //{
        //    try
        //    {
        //        SMTP.Send(NewMessage);
        //    }
        //    finally
        //    {
        //        SMTP = null;
        //        NewMessage = null;
        //        rexEmail = null;
        //    }
        //}
    }
}
